<template>
	<form class="page-index" @submit="sendSubmit">
		<div class="state">
			<!-- <div class="state_txt">
        <text>设备机器码：</text>
        <input
          name="UUID"
          type="text"
          cursor-spacing="50px"
          :value="UUID"
          placeholder="扫描控制器上二维码获取"
        />
      </div>
      <div class="state_txt">
        <text>小程序编码：</text>
        <input
          name="deviceId"
          type="text"
          cursor-spacing="50px"
          :value="deviceId"
          placeholder="支付二维码上编号"
        />
      </div> -->
			<div class="state_txt">
				<text>订单编号：(需大于100000，后续需大于前次)</text>
				<input name="chargeId" type="text" disabled :value="chargeId" cursor-spacing="50px" placeholder="可通过检测设备获取当前编号基础上+1" />
			</div>
			<div class="state_txt">
				<text>通电时长：(分钟)</text>
				<input name="minute" type="text" cursor-spacing="50px" disabled :value="minute" placeholder="" />
			</div>
			<div class="state_txt">TIPS：启动通电须打开蓝牙</div>
			
			<button class="bnt" form-type="submit">启动设备</button>
			
			<!-- <button @click="kongzhi" class="bnt">控制机器人</button> -->
			<!-- <navigator class="bnt" open-type="navigate" hover-class="none" url="/others/pages/control/control">
				<text>控制机器人</text>
			</navigator> -->
			
			<!-- <navigator open-type="navigate" hover-class="none" url="/others/pages/nearby/nearby">
				<text>检测设备</text>
			</navigator> -->
		</div>
	</form>
</template>

<script>
import utils from '@/utils/index';
import { startFw } from '@/api/others.js';
export default {
	name: 'PageIndex',
	data() {
		return {
			id: '',
			UUID: '',
			deviceId: '',
			chargeId: '',
			minute: '',
		};
	},
	onLoad(options) {
		this.id = options.id;
		this.UUID = options.UUID;
		this.deviceId = options.deviceId;
		this.chargeId = options.chargeId;
		this.minute = options.minute;
		// this.UUID = '003CS00g0M0K1+R01';
		//  	this.deviceId = 84709;
		//  	this.chargeId = 18108810;
		//  	this.minute = 80;
	},
	methods: {
		sendSubmit(e) {
			// startFw({id:'105'}).then(res => {
			// 	console.log('启动服务')
			// 	uni.reLaunch({
			// 		url:'/others/pages/control/control'
			// 	})
			// });
			// return
			const id = this.id;
			const UUID = this.UUID;
			const deviceId = this.deviceId;
			const chargeId = this.chargeId;
			const minute = this.minute;

			let checkTrue = true;
			if (UUID.length !== 17) {
				uni.showToast({
					title: '设备机器码错误！',
					icon: 'none'
				});
				checkTrue = false;
			}
			if (checkTrue && Number(deviceId) < 10000) {
				uni.showToast({
					title: '小程序编码错误！',
					icon: 'none'
				});
				checkTrue = false;
			}
			if (checkTrue && Number(chargeId) < 100000) {
				uni.showToast({
					title: '订单编码为空！',
					icon: 'none'
				});
				checkTrue = false;
			}

			if (checkTrue) {
				// 传递对应参数获取加密指令
				const data = {
					UUID,
					device_id: deviceId,
					charge_id: chargeId,
					minute, // 如果需要按秒的，可换参数second；如果minute和second都传0，则为中断通电
					max_power: 0, // 最大功率，单位W，超过则断电
					min_power: 0, // 最小功率，单位W，超过则断电
					quantity: 0, // 电量限制，单位W.h，超过则断电，需要设备本身支持功率检测
					blackout_time: 0, // 拔插判断时间，对带功率检测功能的控制器有效
					delay_time: 60 // 延迟进行拔插/最低功率判断，对带功率检测功能的控制器有效
				};
				utils.postCMD(data, (res) => {
					if (res.cmd) {
						utils.writeChargeData(deviceId, res.cmd, id).catch((_) => false);
					} else if (res.error) {
						uni.showModal({
							content: res.error,
							showCancel: false,
							confirmText: '我知道了'
						});
					}
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.page-index {
	padding-bottom: 130rpx;

	.no_tips {
		text-align: center;
		padding-top: 40%;
		font-size: 34rpx;
	}

	.state {
		padding: 0 20rpx;
	}

	.state_txt {
		padding: 10rpx 0;
		line-height: 80rpx;
		color: #eb9a18;
	}

	.state_txt > text {
		display: inline-block;
		width: 750rpx;
		color: #494949;
	}

	.state_txt > image {
		width: 110rpx;
		vertical-align: middle;
	}

	.state_bnt {
		padding-top: 30rpx;
		text-align: center;
	}

	.payment_txt {
		padding: 10rpx 0;
		line-height: 60rpx;
		color: #eb9a18;
	}

	.payment_txt > text {
		display: inline-block;
		color: #2f4d69;
		font-size: 36rpx;
	}

	.state_bnt button {
		width: 210rpx;
		height: 70rpx;
		border-radius: 70rpx;
		background: #f5a82c;
		color: #fff;
		line-height: 70rpx;
	}

	.state input {
		border: 1px solid #eee;
		border-radius: 8rpx;
		height: 80rpx;
		line-height: 80rpx;
		padding: 10rpx 20rpx;
		background: #fff;
	}

	.state .bnt {
		margin: 25rpx 20rpx 0;
		background: #e9930a;
		color: #fff;
		display: block;
		line-height: 100rpx;
		height: 100rpx;
		border-radius: 10rpx;
	}

	.state navigator {
		margin: 25rpx 20rpx 0;
		background: #442c21;
		color: #fff;
		display: block;
		line-height: 100rpx;
		height: 100rpx;
		border-radius: 10rpx;
		text-align: center;
	}

	.state navigator text {
		display: block;
		color: #fff;
	}
}
</style>